package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.service.AddressFegin;
import com.guigu.qingcheng.service.CartFegin;
import com.guigu.qingcheng.service.OrderFegin;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.order.Order;
import com.qingcheng.pojo.user.Address;
import com.qingcheng.util.JwtUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartFegin cartFegin;

    @Autowired
    private OrderFegin orderFegin;

    @Autowired
    private AddressFegin addressFegin;

    @GetMapping("/findCartList")
    @ResponseBody
    public List<Map<String,Object>> findCartList(HttpServletRequest request){
        String username = null;
        try {
            username = JwtUtils.getMemberIdByJwtToken(request);
        } catch (Exception e) {
            username = "Patrick";
        }
        return cartFegin.findCartList(username);
    }

    @PostMapping("/addItem")
    @ResponseBody
    public Result addItem(String skuId, Integer num,HttpServletRequest request){
        String username = null;
        try {
            username = JwtUtils.getMemberIdByJwtToken(request);
        } catch (Exception e) {
            e.printStackTrace();
            username = "Patrick";
        }
        return cartFegin.addItem(skuId,num,username);
    }

    @PostMapping("/updateChecked")
    @ResponseBody
    public Result updateChecked(String skuId, boolean checked,HttpServletRequest request){
        String username = null;
        try {
            username = JwtUtils.getMemberIdByJwtToken(request);
        } catch (Exception e) {
            e.printStackTrace();
            username = "Patrick";
        }
        return cartFegin.updateChecked(skuId,checked,username);
    }

    @GetMapping("/deleteCart")
    @ResponseBody
    public Result deleteCart(HttpServletRequest request){
        String username = null;
        try {
            username = JwtUtils.getMemberIdByJwtToken(request);
        } catch (Exception e) {
            e.printStackTrace();
            username = "Patrick";
        }
        return cartFegin.deleteCart(username);
    }

    @GetMapping("/preferential")
    @ResponseBody
    public Result preferential(HttpServletRequest request){
        String username = null;
        try {
            username = JwtUtils.getMemberIdByJwtToken(request);
        } catch (Exception e) {
            username = "Patrick";
        }
        return cartFegin.preferential(username);
    }

    @GetMapping("/findNewOrderItemList")
    @ResponseBody
    public List<Map<String,Object>> findNewOrderItemList(HttpServletRequest request){
        String username = null;
        try {
            username = JwtUtils.getMemberIdByJwtToken(request);
        } catch (Exception e) {
            username = "Patrick";
        }
        return cartFegin.findNewOrderItemList(username);
    }

    @GetMapping("/findByUsername")
    @ResponseBody
    public List<Address> findByUsername(){
        return addressFegin.findByUsername("Patrick");
    }

    @PostMapping("/saveOrder")
    @ResponseBody
    public Map<String,Object> saveOrder(@RequestBody Order order,HttpServletRequest request){
        String username = null;
        try {
            username = JwtUtils.getMemberIdByJwtToken(request);
        } catch (Exception e) {
            e.printStackTrace();
            username = "Patrick";
        }
        return orderFegin.saveOrder(order,username);
    }
}
